using Dapper;
using StudentWeb.Common;
using StudentWeb.IDAL;
using StudentWeb.Model;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace StudentWeb.DAL
{
	/// <summary>
	/// WYUserInfo 的数据操作类
	/// </summary>
    [Service(EServLife.Singleton)]
	public partial class WYUserInfoDAO(IConnectionFactory connFactory) : IWYUserInfoDAO
	{
        #region 添加单条记录
        public int Insert(WYUserInfo wyUserInfo, IDbTransaction? trans = null)
        {
            var sql = "INSERT INTO WYUserInfo(WYUserName, WYPassword, WYEmail, WYPhone, WYAddress, WYRegDateTime) VALUES (@WYUserName, @WYPassword, @WYEmail, @WYPhone, @WYAddress, @WYRegDateTime);SELECT CAST(SCOPE_IDENTITY() AS int)";

            if (trans != null && trans.Connection != null)
            {
                var id = trans.Connection.Query<int>(sql, wyUserInfo, trans).Single();
                wyUserInfo.WYUserID = id;
                var result = 1;
                return result;
            }
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                var id = conn.Query<int>(sql, wyUserInfo).Single();
                wyUserInfo.WYUserID = id;
                var result = 1;
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 添加多条记录
        public int Insert(List<WYUserInfo> list, IDbTransaction? trans = null)
        {
            var sql = "INSERT INTO WYUserInfo(WYUserName, WYPassword, WYEmail, WYPhone, WYAddress, WYRegDateTime) VALUES (@WYUserName, @WYPassword, @WYEmail, @WYPhone, @WYAddress, @WYRegDateTime)";
            var result = 0;
            if (trans != null && trans.Connection != null)
            {
                result = trans.Connection.Execute(sql, list, trans);
                return result;
            }
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                result = conn.Execute(sql, list);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 编辑单条记录
        public int Update(WYUserInfo wyUserInfo, string? fields = null, IDbTransaction? trans = null)
        {
            if (string.IsNullOrEmpty(fields))
                fields = "WYUserName = @WYUserName, WYPassword = @WYPassword, WYEmail = @WYEmail, WYPhone = @WYPhone, WYAddress = @WYAddress, WYRegDateTime = @WYRegDateTime";
            else
                fields = DalHelper.FieldsToUpdates(fields);
            var sql = $"UPDATE WYUserInfo SET {fields} WHERE WYUserID = @WYUserID";
            var result = 0;
            if (trans != null && trans.Connection != null)
            {
                result = trans.Connection.Execute(sql, wyUserInfo, trans);
                return result;
            }
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                result = conn.Execute(sql, wyUserInfo);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 编辑多条记录
        public int Update(List<WYUserInfo> list, string? fields = null, IDbTransaction? trans = null)
        {
            if (string.IsNullOrEmpty(fields))
                fields = "WYUserName = @WYUserName, WYPassword = @WYPassword, WYEmail = @WYEmail, WYPhone = @WYPhone, WYAddress = @WYAddress, WYRegDateTime = @WYRegDateTime";
            else
                fields = DalHelper.FieldsToUpdates(fields);
            var sql = $"UPDATE WYUserInfo SET {fields} WHERE WYUserID = @WYUserID";
            var result = 0;
            if (trans != null && trans.Connection != null)
            {
                result = trans.Connection.Execute(sql, list, trans);
                return result;
            }
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                result = conn.Execute(sql, list);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 删除单条记录
        public int Delete(int wYUserID, IDbTransaction? trans = null)
        {
            var sql = "DELETE WYUserInfo WHERE WYUserID = @WYUserID";
            var result = 0;
            if (trans != null && trans.Connection != null)
            {
                result = trans.Connection.Execute(sql, new { WYUserID = wYUserID }, trans);
                return result;
            }
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                result = conn.Execute(sql, new { WYUserID = wYUserID });
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 删除单条记录
        public int Delete(WYUserInfo wyUserInfo, IDbTransaction? trans = null)
        {
            var sql = "DELETE WYUserInfo WHERE WYUserID = @WYUserID";
            var result = 0;
            if (trans != null && trans.Connection != null)
            {
                result = trans.Connection.Execute(sql, wyUserInfo, trans);
                return result;
            }
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                result = conn.Execute(sql, wyUserInfo);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 获取单条记录
        public T? GetInfo<T>(int wYUserID, string? fields = null)
        {
            if (string.IsNullOrEmpty(fields)) { fields = "*"; }
            var sql = $"SELECT {fields} FROM WYUserInfo WHERE WYUserID = @WYUserID";
            using (var conn = connFactory.GetConnection())
            {
                var model = conn.QueryFirstOrDefault<T>(sql, new { WYUserID = wYUserID });
                return model;
            }
        }
        public WYUserInfo? GetInfo(int wYUserID) { return GetInfo<WYUserInfo>(wYUserID); }
        public T? GetInfo<T>(string? whereClause = null, object? param = null, string? fields = null)
        {
            if (string.IsNullOrEmpty(fields)) { fields = "*"; }
            if (!string.IsNullOrEmpty(whereClause))
            {
                whereClause = "WHERE " + whereClause;
            }
            var sql = $"SELECT {fields} FROM WYUserInfo {whereClause}";
            using (var conn = connFactory.GetConnection())
            {
                var model = conn.QueryFirstOrDefault<T>(sql, param);
                return model;
            }
        }
        public WYUserInfo? GetInfo(string? whereClause = null, object? param = null)
        {
            return GetInfo<WYUserInfo>(whereClause, param);
        }
        #endregion
        #region 获取全部记录列表
        public List<T> GetList<T>(string? whereClause = null, object? param = null, string? orderBy = null, string? fields = null)
        {
            if (string.IsNullOrEmpty(fields)) { fields = "*"; }
            if (!string.IsNullOrEmpty(whereClause))
            {
                whereClause = " WHERE " + whereClause;
            }
            if (!string.IsNullOrEmpty(orderBy))
            {
                orderBy = " ORDER BY " + orderBy;
            }
            var sql = $"SELECT {fields} FROM WYUserInfo {whereClause} {orderBy}";
            using (var conn = connFactory.GetConnection())
            {
                var q = conn.Query<T>(sql, param);
                return q.ToList();
            }
        }
        public List<WYUserInfo> GetList(string? whereClause = null, object? param = null, string? orderBy = null)
        {
            return GetList<WYUserInfo>(whereClause, param, orderBy);
        }
        #endregion
        #region 分页列表
        public PagedList<T> GetList<T>(int pageIndex, int pageSize, string? whereClause = null, object? param = null, string? orderBy = null, string? fields = null)
        {
            pageIndex--;
            if (string.IsNullOrEmpty(fields)) { fields = "*"; }
            if (!string.IsNullOrEmpty(whereClause))
            {
                whereClause = " WHERE " + whereClause;
            }
            if (string.IsNullOrEmpty(orderBy))
            {
                orderBy = "WYUserID DESC";
            }
            var startItem = pageIndex * pageSize + 1;
            var endItem = (pageIndex + 1) * pageSize;

            var sql = 
$@"SELECT COUNT(WYUserID) FROM WYUserInfo {whereClause} 
SELECT *
FROM(SELECT ROW_NUMBER() OVER(ORDER BY {orderBy}) AS RowNum, {fields}
          FROM WYUserInfo
          {whereClause}
        ) AS result
WHERE RowNum >= {startItem}
    AND RowNum <= {endItem}
ORDER BY RowNum";
            using (var conn = connFactory.GetConnection())
            {
                conn.Open();
                int count = 0;
                var list = new List<T>();
                using (var q = conn.QueryMultiple(sql, param))
                {
                    count = q.Read<int>().Single();
                    list = q.Read<T>().ToList();
                }
                conn.Close();

                var pagedList = new PagedList<T>(list, pageIndex, pageSize, count);
                return pagedList;
            }
        }
        public PagedList<WYUserInfo> GetList(int pageIndex, int pageSize, string? whereClause = null, object? param = null, string? orderBy = null)
        {
            return GetList<WYUserInfo>(pageIndex, pageSize, whereClause, param, orderBy);
        }
        #endregion
	}
}



